#ifndef QINSERTWIDGET_P_H
#define QINSERTWIDGET_P_H
#include "qinsertwidget.h"
#include "inputwidgetutils.h"
#include <QPushButton>
#include <QLineEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QSqlQuery>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QMessageBox>
#include <QSqlError>

class QInsertWidgetPrivate : public QObject
{
    Q_DECLARE_PUBLIC(QInsertWidget)
public:
    QInsertWidgetPrivate(QInsertWidget *q);

    void _q_insertData();
    void _q_cancel();
    bool initUI();
    bool initData(QSqlDatabase *_db, QTableAttribute *_tableAttribute);
private:
    QInsertWidget *q_ptr;
    QTableAttribute *tableAttribute;
    QPushButton *insertBtn;
    QPushButton *cancelBtn;
    QSqlDatabase *db;
    QVector<QWidget *> inputWidgets;

};

QInsertWidgetPrivate::QInsertWidgetPrivate(QInsertWidget *q)
    : q_ptr(q)
    , tableAttribute(nullptr)
    , insertBtn(new QPushButton("确认",q))
    , cancelBtn(new QPushButton("取消",q))
{

}

void QInsertWidgetPrivate::_q_insertData()
{
    Q_Q(QInsertWidget);
    QString insert_sql = QString("insert into %1").arg(tableAttribute->tableName);
    insert_sql.append("(");
    for(int i = 0 ;i < inputWidgets.size(); i++){
        auto inputWidget = inputWidgets.at(i);
        QString fieldName = inputWidget->property("fieldName").toString();
        insert_sql.append(fieldName);
        if(i != inputWidgets.size()-1){
            insert_sql.append(",");
        }
    }
    insert_sql.append(") values (");

    for(int i = 0 ;i < inputWidgets.size(); i++){
        auto inputWidget = inputWidgets.at(i);
        QString value = InputWidgetUtils::getInputWidgetValue(inputWidget);
        insert_sql.append("\'"+ value +"\'");
        if(i != inputWidgets.size()-1){
            insert_sql.append(",");
        }
    }
    insert_sql.append(");");
    qInfo()<< insert_sql;

    QSqlQuery query(insert_sql,*db);
    if(query.exec(insert_sql)){
        QMessageBox::information(q,"提示","插入数据成功!");
    }
    else{
        QMessageBox::warning (q,"警告","插入数据失败,"+query.lastError().text());
    }
}

void QInsertWidgetPrivate::_q_cancel()
{
    Q_Q(QInsertWidget);
    q->hide();
}

bool QInsertWidgetPrivate::initUI()
{
    Q_Q(QInsertWidget);
    QVBoxLayout *mainLayout = new QVBoxLayout();
    QGridLayout *insertLayout = new QGridLayout();
    QHBoxLayout *btnLayout = new QHBoxLayout();

    QObject::connect(insertBtn,&QPushButton::clicked,this,&QInsertWidgetPrivate::_q_insertData);
    QObject::connect(cancelBtn,&QPushButton::clicked,this,&QInsertWidgetPrivate::_q_cancel);

    int row = 0;
    for(int i = 0 ; i < tableAttribute->fieldAttributes.size();i++){
        auto item = tableAttribute->fieldAttributes.at(i);
        QLabel *lab = new QLabel(item.label);
        QWidget *inputWidget = InputWidgetUtils::createInputWidget(item);
        inputWidgets.push_back(inputWidget);
        insertLayout->addWidget(lab,row,0);
        insertLayout->addWidget(inputWidget,row,1);
        row++;
    }
    mainLayout->addLayout(insertLayout);
    btnLayout->addWidget(insertBtn);
    btnLayout->addWidget(cancelBtn);
    mainLayout->addLayout(btnLayout);
    q->setLayout(mainLayout);
    return true;
}

bool QInsertWidgetPrivate::initData(QSqlDatabase *_db, QTableAttribute *_tableAttribute)
{
    db = _db;
    tableAttribute = _tableAttribute;
    return true;
}



#endif // QINSERTWIDGET_H
